package 剑指Offer;

import java.util.Stack;

public class Offer09_用两个栈实现队列 {
    class CQueue {

        Stack<Integer> A, B;

        public CQueue() {
            A = new Stack<Integer>();
            B = new Stack<Integer>();
        }

        public void appendTail(int value) {
            A.push(value);
        }

        public int deleteHead() {
            if (!B.isEmpty()) {
                return B.pop();
            }
            if (A.isEmpty()) {//上面判断了B不为空，如果来到这里，证明B为空，A也为空
                return -1;
            }
            while (!A.isEmpty()) {//A不为空，就把A的元素弹出到B，再删除B栈顶元素
                B.push(A.pop());
            }
            return B.pop();
        }
    }

/**
 * Your CQueue object will be instantiated and called as such:
 * CQueue obj = new CQueue();
 * obj.appendTail(value);
 * int param_2 = obj.deleteHead();
 */
}
